package com.ruicar.afs.cloud.afscase.loangpsruleinfo.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.loangpsruleinfo.entity.LoanGpsRuleInfo;
import com.ruicar.afs.cloud.afscase.loangpsruleinfo.mapper.LoanGpsRuleInfoMapper;
import com.ruicar.afs.cloud.afscase.loangpsruleinfo.service.LoanGpsRuleInfoService;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: yk.Li
 * @date 2020-07-22 05:38:24
 * @description GPS安装规则配置表 service实现类
 */
@Service
@AllArgsConstructor
@Data
@Slf4j
public class LoanGpsRuleInfoServiceImpl extends ServiceImpl<LoanGpsRuleInfoMapper, LoanGpsRuleInfo> implements LoanGpsRuleInfoService {
    private AfsRuleInfoService afsRuleInfoService;

    /**
     * @param
     * @description 通过业务规则id批量失效规则
     * @author yk.Li
     * @date 2020/7/6
     */
    @Override
    public void deActiveRuleByRuleNo(Long[] ids) {
        //使规则失效
        for (Long id : ids) {
            LoanGpsRuleInfo loanGpsRuleInfo = this.getById(id);
            if (null != loanGpsRuleInfo) {
                AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanGpsRuleInfo.getRuleId());
                if (null != afsRuleInfo) {
                    afsRuleInfoService.deActiveRule(afsRuleInfo.getId());//规则失效
                    log.info("--------------------规则【" + afsRuleInfo.getId() + "】已失效----------------->");
                }
            }
        }
    }

    /**
     * @param
     * @description 停用规则
     * @author yk.Li
     * @date 2020/7/6
     */
    @Override
    public void deActiveRule(Long id) {
        //设为停用
        LoanGpsRuleInfo loanGpsRuleInfo = this.getById(id);
        if (null != loanGpsRuleInfo) {
            loanGpsRuleInfo.setIsEnable(WhetherEnum.NO.getCode());
            loanGpsRuleInfo.setFailureTime(new Date());
            this.updateById(loanGpsRuleInfo);
            //设置规则失效
            AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanGpsRuleInfo.getRuleId());
            if (null != afsRuleInfo) {
                afsRuleInfoService.deActiveRule(afsRuleInfo.getId());//规则失效
            }
        }
    }

    /**
     * @param
     * @description 启用规则
     * @author yk.Li
     * @date 2020/7/6
     */
    @Override
    public void activeRule(Long id) {
        //设为启用
        LoanGpsRuleInfo loanGpsRuleInfo = this.getById(id);
        if (null != loanGpsRuleInfo) {
            loanGpsRuleInfo.setIsEnable(WhetherEnum.YES.getCode());
            loanGpsRuleInfo.setEffectTime(new Date());
            this.updateById(loanGpsRuleInfo);
            //激活规则
            AfsRuleInfo afsRuleInfo = afsRuleInfoService.getById(loanGpsRuleInfo.getRuleId());
            if (null != afsRuleInfo) {
                afsRuleInfoService.activeRule(afsRuleInfo.getId());//规则生效
            }
        }
    }

    /**
     * @param
     * @description 更新同步状态 同步时间
     * @author yk.Li
     * @date 2020/7/24
     */
    @Override
    public void updateGpsRulesInfo(Long id) {
        this.update(Wrappers.<LoanGpsRuleInfo>lambdaUpdate()
                .eq(LoanGpsRuleInfo::getId, id)
                .set(LoanGpsRuleInfo::getIsSynchronous, WhetherEnum.YES.getCode())
                .set(LoanGpsRuleInfo::getSynchronousTime, new Date()));
    }
}
